package com.cskaoyan.service;

import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.utils.MyPageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AdminLogServiceImpl
 * @Description
 * @Author Lxz
 * @Date 2022-8-13 9:01
 */
@Service
public class AdminLogServiceImpl implements  AdminLogService{
    @Autowired
    MarketLogMapper marketLogMapper;
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketOrderMapper orderMapper;

    @Override
    public CommonData list(String username,BasePageInfo info) {
        MyPageHelper.startPage(info);
        MarketLogExample example = new MarketLogExample();
        if (info.getOrder() != null && info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketLogExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andAdminLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketLog> marketLogs = marketLogMapper.selectByExample(example);

        PageInfo<MarketLog> marketLogPageInfo = new PageInfo<>(marketLogs);
        CommonData data = CommonData.data(marketLogPageInfo);
        return data;

    }

    @Override
    public MarketAdmin getAdminInfo(MarketAdmin primaryPrincipal) {
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(primaryPrincipal.getUsername());
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        return marketAdmins.get(0);
    }

    @Override
    public MarketOrder getOrderInfo(MarketOrder marketOrder) {
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria().andIdEqualTo(marketOrder.getId());
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example);
        return marketOrders.get(0);
    }

    @Override
    public void insertLog(MarketLog marketLog) {
        marketLogMapper.insertSelective(marketLog);
    }
}
